﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Traceability.Domain.FarmingMsgs;
using Traceability.Domain.Goodss;
using Traceability.Domain.Ingredients;
using Traceability.Domain.Machinings;
using Traceability.Domain.Slaughters;
using Traceability.Repository;
using Traceability.Util;

namespace Traceability.Services.GoodServices
{
    /// <summary>
    /// 产品服务
    /// </summary>
    public class ProductService : IProductService
    {
        private readonly IBaseRepository<Product> _productRepository;
        private readonly IBaseRepository<Goods> _goodsRepository;
        //屠宰信息
        private readonly IBaseRepository<Cultivation> _cultivationRepository;
        /// <summary>
        /// 免疫仓储
        /// </summary>
        private readonly IBaseRepository<Immunity> _immunityRepository;
        //企业
        private readonly IBaseRepository<Enterprise> _enterpriseRepository;

        //屠宰
        private readonly IBaseRepository<Slaughter> _slaughterRepository;
        //深加载企业
        private readonly IBaseRepository<MachiningEnterprise> _machiningEnterpriseRepository;

        //原料
        private readonly IBaseRepository<Material> _materialRepository;

        //辅料
        private readonly IBaseRepository<Accessories>  _accessoriesRepository;
        //添加剂
        private readonly IBaseRepository<Additive> _additiveRepository;
        //包材
        private readonly IBaseRepository<PackeMaterial> _packingRepository;
        

        private IMapper mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productRepository"></param>
        /// <param name="goodsRepository"></param>
        public ProductService(IBaseRepository<Product> productRepository, IBaseRepository<Goods> goodsRepository, IMapper mapper, IBaseRepository<Cultivation> cultivationRepository = null, IBaseRepository<Immunity> immunityRepository = null, IBaseRepository<Enterprise> enterpriseRepository = null, IBaseRepository<Slaughter> slaughterRepository = null, IBaseRepository<MachiningEnterprise> machiningEnterpriseRepository = null, IBaseRepository<Material> materialRepository = null, IBaseRepository<Accessories> accessoriesRepository = null, IBaseRepository<Additive> additiveRepository = null, IBaseRepository<PackeMaterial> packingRepository = null)
        {
            _productRepository = productRepository;
            _goodsRepository = goodsRepository;
            this.mapper = mapper;
            _cultivationRepository = cultivationRepository;
            _immunityRepository = immunityRepository;
            _enterpriseRepository = enterpriseRepository;
            _slaughterRepository = slaughterRepository;
            _machiningEnterpriseRepository = machiningEnterpriseRepository;
            _materialRepository = materialRepository;
            _accessoriesRepository = accessoriesRepository;
            _additiveRepository = additiveRepository;
            _packingRepository = packingRepository;
        }

        /// <summary>
        /// 获取产品列表
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public async Task<APIResult<PageIng<ProductDto>>> GetProducts(int PageIndex, int PageSize)
        {


            var _products = await _productRepository.GetWhereAsync(a => a.IsDelete == false);
            var goods = await _goodsRepository.GetAllAsync();
            var list = from p in _products
                       join g in goods on p.GoodsId equals g.GoodsId
                       select new ProductDto
                       {
                           ProductId = p.ProductId,
                           GoodsId = p.GoodsId,
                           GoodsName = g.GoodsName,
                           ProductBath = p.ProductBath,
                           ProductDay = p.ProductDay,
                           ProductNumber = p.ProductNumber,
                           ProductCompany = p.ProductCompany,
                           ProductFig = p.ProductFig,
                           ProductCode = p.ProductCode,
                       };

            var ping = new PageIng<ProductDto>()
            {
                Data = list.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList(),
                TotalCount = list.Count(),
                TotalPage = (int)Math.Ceiling(list.Count() / (double)PageSize)
            };

            return new APIResult<PageIng<ProductDto>>()
            {
                Data = ping,
                Code = EnumCode.Success,
                Msg = "获取产品列表成功"
            };
        }
        /// <summary>
        /// 新增产品
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> ProductInsert(ProductInsertDto entity)
        {

            var productentity = mapper.Map<Product>(entity);
            productentity.ProductCode = Guid.NewGuid().ToString();
            productentity.UpdatePerson = "";
            productentity.UpdateTime = DateTime.Now;
            productentity.DeleteTime = DateTime.Now;

            var result = await _productRepository.InsertDataAsync(productentity);
            return new APIResult<int>()
            {
                Data = result,
                Code = EnumCode.Success,
                Msg = "新增产品成功"
            };
        }

        /// <summary>
        /// 查询商品 
        /// </summary>
        /// <returns></returns>
        public async Task<APIResult<List<Goods>>> GetGoods()
        {
            var goods = await _goodsRepository.GetAllAsync();

            return new APIResult<List<Goods>>()
            {
                Data = goods.ToList(),
                Code = EnumCode.Success,
                Msg = "获取产品列表成功"
            };
        }

        /// <summary>
        /// 产品修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> ProductUpdate(int id)
        {
            var productentity = await _productRepository.GetAsync(id);
            productentity.ProductId = id;
            productentity.IsDelete = true;
            productentity.GoodsId = productentity.GoodsId;
            productentity.ProductBath = productentity.ProductBath;
            productentity.ProductDay = productentity.ProductDay;
            productentity.ProductNumber = productentity.ProductNumber;
            productentity.ProductCompany = productentity.ProductCompany;
            productentity.ProductFig = productentity.ProductFig;
            productentity.ProductCode = productentity.ProductCode;
            productentity.CreatePerson = productentity.CreatePerson;
            productentity.UpdatePerson = productentity.UpdatePerson;
            productentity.UpdateTime = productentity.UpdateTime;
            productentity.DeleteTime = DateTime.Now;
            await _productRepository.UpdateDataAsync(productentity);
            if (productentity == null)
            {
                return new APIResult<int>()
                {
                    Code = EnumCode.Fail,
                    Msg = "产品不存在"
                };
            }
            return new APIResult<int>()
            {
                Code = EnumCode.Success,
                Msg = "修改产品成功"
            };
        }


        /// <summary>
        /// 修改产品
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> ProductUpdate(ProductUpdateDto entity)
        {

            //修改

            var productentity = await _productRepository.GetAsync(entity.ProductId);
            productentity.ProductId = productentity.ProductId;
            productentity.GoodsId = entity.GoodsId;
            productentity.ProductBath = entity.ProductBath;
            productentity.ProductDay = entity.ProductDay;
            productentity.ProductNumber = entity.ProductNumber;
            productentity.ProductCompany = entity.ProductCompany;
            productentity.ProductCode = productentity.ProductCode;
            productentity.ProductFig = entity.ProductFig;
            productentity.CreatePerson = productentity.CreatePerson;
            productentity.UpdatePerson = entity.UpdatePerson;
            productentity.UpdateTime = DateTime.Now;
            var dto = mapper.Map<Product>(productentity);
            await _productRepository.UpdateDataAsync(dto);
            if (productentity == null)
            {
                return new APIResult<int>()
                {
                    Code = EnumCode.Fail,
                    Msg = "产品不存在"
                };
            }
            return new APIResult<int>()
            {
                Code = EnumCode.Success,
                Msg = "修改产品成功"
            };
        }

        /// <summary>
        /// 查询产品和商品名称
        /// </summary>
        /// <returns></returns>
        public async Task<APIResult<List<ProductDto>>> GetProductAndGoodsName(int id)
        {
            var _products = await _productRepository.GetWhereAsync(a => a.IsDelete == false && a.ProductId == id);
            var goods = await _goodsRepository.GetAllAsync();
            var list = from p in _products
                       join g in goods on p.GoodsId equals g.GoodsId
                       select new ProductDto
                       {
                           ProductId = p.ProductId,
                           GoodsId = p.GoodsId,
                           GoodsName = g.GoodsName,
                           ProductBath = p.ProductBath,
                           ProductDay = p.ProductDay,
                           ProductNumber = p.ProductNumber,
                           ProductCompany = p.ProductCompany,
                           ProductFig = p.ProductFig,
                           ProductCode = p.ProductCode,

                       };

            return new APIResult<List<ProductDto>>()
            {
                Data = list.ToList(),
                Code = EnumCode.Success,
                Msg = "获取产品列表成功"
            };
        }

        /// <summary>
        /// 新增养殖信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> CultivationInsert(CultivationInsertDto entity)
        {
            var cultivationentity = mapper.Map<Cultivation>(entity);
            cultivationentity.ProductCode = Guid.NewGuid().ToString();
            cultivationentity.CreatePerson = "admin";
            cultivationentity.CreateTime = DateTime.Now;
            var result = await _cultivationRepository.InsertDataAsync(cultivationentity);
            return new APIResult<int>()
            {
                Data = result,
                Code = EnumCode.Success,
                Msg = "新增养殖信息成功"
            };
        }
        /// <summary>
        /// 修改养殖信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> CultivationUpdate(CultivationUpdateDto entity)
        {
            var cultivationentity = await _cultivationRepository.GetAsync(entity.CultivationId);
            cultivationentity.CultivationId = cultivationentity.CultivationId;
            cultivationentity.ProductCode = cultivationentity.ProductCode;
            cultivationentity.CultivationName = entity.CultivationName;
            cultivationentity.CulBreed = entity.CulBreed;
            cultivationentity.CulSource = entity.CulSource;
            cultivationentity.CultivationDays = entity.CultivationDays;
            cultivationentity.CultivationBrand = entity.CultivationBrand;
            cultivationentity.SaleDate = entity.SaleDate;
            var dto = mapper.Map<Cultivation>(cultivationentity);
            await _cultivationRepository.UpdateDataAsync(dto);
            if (cultivationentity == null)
            {
                return new APIResult<int>()
                {
                    Code = EnumCode.Fail,
                    Msg = "修改养殖信息不存在"
                };
            }
            return new APIResult<int>()
            {
                Code = EnumCode.Success,
                Msg = "修改养殖信息成功"
            };
        }
        /// <summary>
        /// 免疫信息查询所有
        /// </summary>
        /// <returns></returns>
        public async Task<APIResult<List<Immunity>>> GetImmunity()
        {
            var immunity = await _immunityRepository.GetAllAsync();
            return new APIResult<List<Immunity>>()
            {
                Data = immunity.ToList(),
                Code = EnumCode.Success,
                Msg = "获取免疫信息成功"
            };
        }

        /// <summary>
        /// 根据商品编号查询养殖信息
        /// </summary>
        /// <param name="productCode"></param>
        /// <returns></returns>
        public async Task<APIResult<List<Cultivation>>> GetCultivationByProductCode(string productCode)
        {
            var cultivation = await _cultivationRepository.GetWhereAsync(a => a.ProductCode == productCode);
            return new APIResult<List<Cultivation>>()
            {
                Data = cultivation.ToList(),
                Code = EnumCode.Success,
                Msg = "获取养殖信息成功"
            };
        }
        /// <summary>
        /// 添加免疫信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> ImmunityInsert(ImmunityInsertDto entity)
        {
            var immunityentity = mapper.Map<Immunity>(entity);
            immunityentity.CreatePerson = "admin";
            immunityentity.CreateTime = DateTime.Now;
            var result = await _immunityRepository.InsertDataAsync(immunityentity);
            return new APIResult<int>()
            {
                Data = result,
                Code = EnumCode.Success,
                Msg = "新增免疫信息成功"
            };
        }

        /// <summary>
        /// 根据商品编号查询企业信息
        /// </summary>
        /// <param name="productCode"></param>
        /// <returns></returns>
        public async Task<APIResult<List<Enterprise>>> GetEnterpriseByProductCode(string productCode)
        {
            var enterprise = await _enterpriseRepository.GetWhereAsync(a => a.ProductCode == productCode);
            return new APIResult<List<Enterprise>>()
            {
                Data = enterprise.ToList(),
                Code = EnumCode.Success,
                Msg = "获取企业信息成功"
            };
        }
        /// <summary>
        /// 根据商品编号查询屠宰信息
        /// </summary>
        /// <param name="productCode"></param>
        /// <returns></returns>
        public async Task<APIResult<List<Slaughter>>> GetSlaughterByProductCode(string productCode)
        {
            var slaughter = await _slaughterRepository.GetWhereAsync(a => a.ProductCode == productCode);
            return new APIResult<List<Slaughter>>()
            {
                Data = slaughter.ToList(),
                Code = EnumCode.Success,
                Msg = "获取屠宰信息成功"
            };
        }

        /// <summary>
        /// 企业信息添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> EnterpriseInsert(EnterpriseInsertDto entity)
        {
            var enterpriseentity = mapper.Map<Enterprise>(entity);
            enterpriseentity.ProductCode = Guid.NewGuid().ToString();
            enterpriseentity.CreatePerson = "admin";
            enterpriseentity.CreateTime = DateTime.Now;
            var result = await _enterpriseRepository.InsertDataAsync(enterpriseentity);
            return new APIResult<int>()
            {
                Data = result,
                Code = EnumCode.Success,
                Msg = "新增企业信息成功"
            };
        }

        /// <summary>
        /// 屠宰信息添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> SlaughterInsert(SlaughterInsertDto entity)
        {
            var slaughterentity = mapper.Map<Slaughter>(entity);
            slaughterentity.ProductCode = Guid.NewGuid().ToString();
            slaughterentity.CreatePerson = "admin";
            slaughterentity.CreateTime = DateTime.Now;
            var result = await _slaughterRepository.InsertDataAsync(slaughterentity);
            return new APIResult<int>()
            {
                Data = result,
                Code = EnumCode.Success,
                Msg = "新增屠宰信息成功"
            };
        }

        /// <summary>
        /// 深加工企业信息添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> MachiningEnterpriseInsert(MachiningEnterpriseInsertDto entity)
        {
            var machiningEnterpriseentity = mapper.Map<MachiningEnterprise>(entity);
            machiningEnterpriseentity.ProductCode = Guid.NewGuid().ToString();
            machiningEnterpriseentity.CreatePerson = "admin";
            machiningEnterpriseentity.CreateTime = DateTime.Now;
            var result = await _machiningEnterpriseRepository.InsertDataAsync(machiningEnterpriseentity);
            return new APIResult<int>()
            {
                Data = result,
                Code = EnumCode.Success,
                Msg = "新增深加工企业信息成功"
            };
        }
        /// <summary>
        /// 根据商品编号查询深加工
        /// </summary>
        /// <param name="productCode"></param>
        /// <returns></returns>
        public async Task<APIResult<List<MachiningEnterprise>>> GetMachiningEnterpriseByProductCode(string productCode)
        {
            var machiningEnterprise = await _machiningEnterpriseRepository.GetWhereAsync(a => a.ProductCode == productCode);
            return new APIResult<List<MachiningEnterprise>>()
            {
                Data = machiningEnterprise.ToList(),
                Code = EnumCode.Success,
                Msg = "获取深加工企业信息成功"
            };
        }

        /// <summary>
        /// 根据商品编号查询原料信息
        /// </summary>
        /// <param name="productCode"></param>
        /// <returns></returns>
        public async Task<APIResult<List<Material>>> GetMaterialByProductCode(string productCode)
        {
            var material = await _materialRepository.GetWhereAsync(a => a.ProductCode == productCode);
            return new APIResult<List<Material>>()
            {
                Data = material.ToList(),
                Code = EnumCode.Success,
                Msg = "获取原料信息成功"
            };
        }

        /// <summary>
        /// 根据商品编号查询辅料
        /// </summary>
        /// <param name="productCode"></param>
        /// <returns></returns>
        public async Task<APIResult<List<Accessories>>> GetAccessoryByProductCode(string productCode)
        {
            var accessory = await _accessoriesRepository.GetWhereAsync(a => a.ProductCode == productCode);
            return new APIResult<List<Accessories>>()
            {
                Data = accessory.ToList(),
                Code = EnumCode.Success,
                Msg = "获取辅料信息成功"
            };
        }
        /// <summary>
        /// 根据商品编号查询添加剂信息
        /// </summary>
        /// <param name="productCode"></param>
        /// <returns></returns>
        public async Task<APIResult<List<Additive>>> GetAdditiveByProductCode(string productCode)
        {
            var additive = await _additiveRepository.GetWhereAsync(a => a.ProductCode == productCode);
            return new APIResult<List<Additive>>()
            {
                Data = additive.ToList(),
                Code = EnumCode.Success,
                Msg = "获取添加剂信息成功"
            };
        }
        /// <summary>
        /// 根据商品编号查询包材信息
        /// </summary>
        /// <param name="productCode"></param>
        /// <returns></returns>
        public async Task<APIResult<List<PackeMaterial>>> GetPackingByProductCode(string productCode)
        {
            var packing = await _packingRepository.GetWhereAsync(a => a.ProductCode == productCode);
            return new APIResult<List<PackeMaterial>>()
            {
                Data = packing.ToList(),
                Code = EnumCode.Success,
                Msg = "获取包材信息成功"
            };
        }
        
    }

}

